package com.zjeav.lingjiao.ui.personCenter;

import com.zjeav.lingjiao.base.baseBean.Account;
import com.zjeav.lingjiao.base.baseBean.Book;
import com.zjeav.lingjiao.base.baseBean.Metadata;
import com.zjeav.lingjiao.base.baseBean.Order;
import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.listener.CommonListener;
import com.zjeav.lingjiao.base.service.AccountServicce;
import com.zjeav.lingjiao.base.service.FavoritesAudiosService;
import com.zjeav.lingjiao.base.service.FavoritesBookService;
import com.zjeav.lingjiao.base.service.OrderService;
import com.zjeav.lingjiao.base.service.SavePrifileService;
import com.zjeav.lingjiao.base.tools.ObservableServiceTool;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;
import com.zjeav.lingjiao.ui.personCenter.view.PersonInfoContract;

import java.util.ArrayList;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class PersonPresenter implements CommonListener {
    private PersonInfoContract.View personinfoView;
    private PersonInfoContract.OrderView orderView;
    private PersonInfoContract.FavoriteView favoriteView;
    int mType;


    public PersonPresenter(PersonInfoContract.View personinfoView) {
        this.personinfoView = personinfoView;
    }


    public PersonPresenter(PersonInfoContract.OrderView orderView) {
        this.orderView = orderView;
    }

    public PersonPresenter(PersonInfoContract.FavoriteView favoriteView) {
        this.favoriteView = favoriteView;
    }

    public void getAccount() {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        AccountServicce service = retrofit.create(AccountServicce.class);
        ObservableServiceTool.getResult((Observable) service.getAccount(), 0, this);
    }

    public void getOrders() {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        OrderService service = retrofit.create(OrderService.class);
        ObservableServiceTool.getResult((Observable) service.getOrders(), 1, this);
    }

    public void getFavorites() {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        FavoritesAudiosService service = retrofit.create(FavoritesAudiosService.class);
        ObservableServiceTool.getResult((Observable) service.getFavoriteAudios(), 2, this);
    }

    public void upPersonInfo(Account account, int type) {
        mType = type;
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        SavePrifileService service = retrofit.create(SavePrifileService.class);
        ObservableServiceTool.getResult((Observable) service.saveprofile(account), 3, this);
    }

    @Override
    public void OnSuccess(Result result, int type) {
        if (type == 0) {
            this.personinfoView.showPersonInfo((Account) result.getData());
        } else if (type == 1) {
            this.orderView.showOrders((ArrayList<Order>) result.getData());
        } else if (type == 2) {
            this.favoriteView.showFavorites((ArrayList<Metadata>) result.getData());
        } else if (type == 3) {
            this.personinfoView.showUpdate(result, mType);
        }
    }

    @Override
    public void OnFailure(Throwable throwable, int type) {
        if (type == 0) {
            this.personinfoView.showError(throwable);
        } else if (type == 1) {
            this.orderView.showError(throwable);
        } else if (type == 2) {
            this.favoriteView.showError(throwable);
        }
    }
}
